Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
#### The Database-Program Link A complex chain of links exists between the database table and what the user sees and does in the program. The complexity lies in the details that go on behind the scenes. The parts handled by the programmer are actually relatively simple. The first link, the one closest to the database, is the Jet database engine. The programmer rarely has to be concerned with the engine—it’s just there, doing its job. The next link is the Data control. This is a specialized control whose task is to serve as a link between your program and a database table. The Data control is placed on a form like any other control. It displays as a horizontal bar with arrows at either end, as shown in Figure 19.3. To use a data control, you must “attach” it to a database file and to a specific table within that file. Then the Data control will allow you to browse through the records in the table using its arrows and to manipulate the table data using its methods. Figure 19.3 A Data control. The third link in the chain between user and database is one or more data-aware controls (sometimes called data-bound controls). A data-aware control can be linked to either a specific Data control or a specific field in the table to which the Data control is linked. Once the link is in place, data is automatically transferred back and forth between the database table and the control. If you move to a certain record in the database, that record’s data is displayed in the control. If you enter new data in the control, the new data is saved in the database. All this is handled automatically, with little or no need for programming. The links between a data aware control and a database table are diagrammed in Figure 19.4. Several of Visual Basic’s controls are data aware. The Check Box, Text Box, Label, Picture, Image, List Box, OLE, and Combo Box controls can be bound to a single field of a table linked to the Data control. The MaskedEdit and 3DCheckBox controls, available in the Professional Edition of Visual Basic, are also data aware. These controls deal with a single record at a time. The DataList, DataCombo, DataGrid, and MSHFlexGrid controls, which are also data aware, are used to display or manipulate several records at once. Figure 19.4 The links between a data-aware control and a database table. Except for the last four controls, the data-aware controls do not have to be used in conjunction with a Data control. In other words, their data-awareness is an option. #### Designing The Database To demonstrate the Jet database engine and the associated controls, I wanted to design a database similar to the address list database that we created earlier in the chapter using traditional Basic programming methods. Rather than creating another address list, however, I thought we should try something different. Looking around my living room, I came up with the answer: my scattered collection of more than 1,000 LPs and CDs, in serious need of organization. A database to keep track of my recordings and their shelf locations would be a good start. While different in detail from the address database developed earlier, the musical recordings database will be similar enough in basic structure and functionality to provide a good comparison. #### Using The Visual Data Manager To Create The Database The first step is to create the blank database the program will use. This database will have the table and field structure required to keep track of the LPs and CDs. Then, once the database exists, we can write a Visual Basic program to access and manipulate it. To begin, select Visual Data Manager from the Add-Ins menu. VisData will start and display a blank window. Select New from the File menu. The next menu lets you select the database format: You should select Microsoft Access, then Version 7 MDB from the next menu. Unless you have a specific reason to use a different database file format, I suggest you use this one for your new databases. Next, the New Database dialog box is displayed. Select the folder where you want the database located. For our purposes, this should be the folder where the Visual Basic project will be placed. Type the database name “Music” in the File name box, and click on the Save button. The Visual Data Manager will now display the Database window, with the single entry of Properties. Right-click on Properties and select New Table from the pop-up menu. VisData will display the Table Structure dialog box, which is shown in Figure 19.5. Figure 19.5 The Table Structure dialog box. In the Table Name box at the top of the dialog box, type the table name. We’ll call it “Recordings”. Next, use this same dialog box to define the fields that will be in this table. Follow these steps for each field: 1. Click on the Add Field button to display the Add Field dialog box (Figure 19.6). 2. Type the field name in the Name box. 3. Pull down the Type list and select the data type of the field. You have all of Visual Basic’s data types available, plus a couple of new ones (don’t worry about these just yet). Figure 19.6 The Add Field dialog box. 4. If you are creating a field with the Text data type, specify the length of the field in the Size box (the maximum number of characters it can contain). A Size value will display for other data types, but this is for your information only; you can’t change it. 5. Click on OK. 6. Repeat Steps 2 through 5 to define additional fields. When you have defined all of the fields in your database table, click on Close. You’ll notice that several other options are available in the Add Field dialog box. While you do not need them for this example, you need to know about a couple of them: • AutoIncrField—Available only if the field is a type Long. When selected, the database engine will automatically fill the field with unique, sequential numbers for each record. This option is useful when you need a field that will uniquely identify each record. • AllowZeroLength—For Text type fields only. If selected, a zero-length string is considered a valid entry for the field. • Required—For all field types. If selected, the database engine will not allow the field to be left empty (null). When you close the Add Field dialog box, the fields you defined will be listed in the Field List section of the Table Structure dialog box. To delete a field, click on it and select Delete. To change a field definition, you must first delete it, then use the Add Field command to redefine it.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.